var express = require('express');
var router = express.Router();
const {User} = require('../../models')
const {success,failure} = require('../../util/requestResult')
const {NotFoundError,BadRequestError,UnauthorizedError,AlreadyExistsError} = require('../../util/errors')

router.post('/',async function(req,res,next){
    try {
        let userinfo = filterBody(req)
        //res.json(userinfo);
        
        let user = await User.findOne({where:{account:userinfo.account}})
        if(user)
            throw new AlreadyExistsError("此账号已存在");
        let createdUser = await User.create(userinfo)
        success(res,'创建用户成功',createdUser,201)
    } catch (error) {
        failure(res,error)
    }

})

function filterBody(req){
    let {name,account,password,role} = req.body
    return {name,account,password,role}
}

module.exports = router